package com.hoopchina.weibo.ui;

import java.util.ArrayList;
import java.util.List;

import com.hoopchina.meta.BindItem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

/**
 * 
 * @author neusoft
 * 
 */
public class BindingDBEngine {
	private Context mContext;
	BindingDBHelper mDBHelper;

	public BindingDBEngine(Context context) {
		mContext = context;
		mDBHelper = new BindingDBHelper(mContext, "news_mybinding_db");
		mDBHelper.getReadableDatabase();
	}

	/**
	 * 获取绑定列表
	 */
	public List<BindItem> getBindingList() {
		Cursor cursor = null;
		List<BindItem> list = new ArrayList<BindItem>();
		try {
			SQLiteDatabase db = mDBHelper.getWritableDatabase();
			cursor = db.rawQuery("select * from my_binding_table;", null);
			while (cursor.moveToNext()) {
				BindItem item = new BindItem();
				String screenName = cursor.getString(cursor
						.getColumnIndex("screenname"));
				String from = cursor.getString(cursor
						.getColumnIndex("fromsite"));
				String picUrl = cursor.getString(cursor
						.getColumnIndex("headurl"));
				int type = cursor.getInt(cursor.getColumnIndex("type"));
				item.setScreenName(screenName);
				item.setSite(from);
				item.setHeadUrl(picUrl);
				item.setType(type);
				list.add(item);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} finally {
			if (cursor != null)
			{
				cursor.close();
			}
			if (null != mDBHelper) {
				mDBHelper.close();
			}
		}
		return list;
	}

	/**
	 * 获取绑定数据
	 */

	public BindItem getBindItem(int weiboType) {
		BindItem item = null;
		Cursor cursor = null;
		try {
			SQLiteDatabase db = mDBHelper.getWritableDatabase();
			cursor = db.rawQuery("select * from my_binding_table;", null);
			while (cursor.moveToNext()) {
				int type = cursor.getInt(cursor.getColumnIndex("type"));
				String screenName = cursor.getString(cursor
						.getColumnIndex("screenname"));
				String from = cursor.getString(cursor
						.getColumnIndex("fromsite"));
				String picUrl = cursor.getString(cursor
						.getColumnIndex("headurl"));
				String token = cursor.getString(cursor.getColumnIndex("token"));
				String tokenSecret = cursor.getString(cursor
						.getColumnIndex("tokensecret"));

				if (type == weiboType) {
					item = new BindItem();
					item.setType(type);
					item.setScreenName(screenName);
					item.setSite(from);
					item.setHeadUrl(picUrl);
					item.setAccessToken(token);
					item.setTokenSecret(tokenSecret);
					break;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} finally {
			if (cursor != null)
				cursor.close();
			if (null != mDBHelper) {
				mDBHelper.close();
			}
		}
		return item;
	}
	
	/**
	 * 判断是否绑定
	 */
	public boolean isBinding(int weiboType) {
		boolean binding = false;
		Cursor cursor = null;
		try {
			SQLiteDatabase db = mDBHelper.getWritableDatabase();
			cursor = db.rawQuery("select * from my_binding_table;", null);
			while (cursor.moveToNext()) {
				int type = cursor.getInt(cursor.getColumnIndex("type"));
				if (type == weiboType) {
					binding = true;
					break;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} finally {
			if (cursor != null)
				cursor.close();
			if (null != mDBHelper) {
				mDBHelper.close();
			}
		}
		return binding;
	}

	/**
	 * 取消一条绑定数据
	 */
	public void removeBindingItem(int type) {
		try {
			SQLiteDatabase db = mDBHelper.getWritableDatabase();
			String str = "DELETE FROM my_binding_table WHERE type=" + type;
			db.execSQL(str);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (null != mDBHelper) {
				mDBHelper.close();
			}
		}
	}
	
	/**
	 * 添加绑定数据
	 */
	public void addBindingItem(BindItem aBindItem) {
		try {
			ContentValues value = new ContentValues();
			SQLiteDatabase db = mDBHelper.getWritableDatabase();
			value.put("type", aBindItem.getType());
			value.put("screenname", aBindItem.getScreenName());
			value.put("fromsite", aBindItem.getSite());
			value.put("headurl", aBindItem.getHeadUrl());
			value.put("token", aBindItem.getAccessToken());
			value.put("tokensecret", aBindItem.getTokenSecret());
			db.insert("my_binding_table", null, value);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (null != mDBHelper) {
				mDBHelper.close();
			}
		}
	}
}
